import { GetAddress, PostAddress } from "../../api/address";
import { GetCart } from "../../api/cart";
import { PostOrder } from "../../api/order3c";
import { err } from "../../utils/console";
import { toast } from "../../utils/wxapi";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    address_id: "",
    show: false,
    item: {},
    cart_list: [],
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.addresslist();
    this.getcart();
  },
  async getcart() {
    try {
      const data = { all: 1 };
      const res: any = await GetCart(data);
      console.log(res);
      if (res.statusCode === 200) {
        this.setData({ cart_list: res.data });
      } else {
        err(res.data);
      }
    } catch (error) {
      err(error);
    }
  },
  async addresslist() {
    try {
      const res = await GetAddress({ all: 1 });
      console.log(res);
      if (res.statusCode === 200) {
        this.setData({ list: res.data });
      } else {
        err(res.data);
      }
    } catch (error) {
      err(error);
    }
  },
  async postAddress() {
    try {
      const res: any = await this.chooseAddressAsync();
      if (res.errMsg === "chooseAddress:ok") {
        const {
          cityName,
          countyName,
          detailInfo,
          provinceName,
          telNumber,
          userName,
        } = res;
        const data = {
          name: userName,
          phone: telNumber,
          address: provinceName + cityName + countyName + detailInfo,
        };
        const resp: any = await PostAddress(data);
        console.log(resp);
        if (resp.statusCode === 200) {
          toast("添加成功");
          this.addresslist();
        }
      }
    } catch (error) {
      err(error);
    }
  },
  chooseAddressAsync(): Promise<any> {
    return new Promise((resolve, reject) => {
      wx.chooseAddress({
        success: resolve,
        fail: reject,
      });
    });
  },
  address() {
    this.setData({ show: true });
  },
  seladd(e: any) {
    const { item } = e.currentTarget.dataset;
    console.log(item);
    this.setData({ item, address_id: item.id, show: false });
  },
  onClose() {
    this.setData({ show: false });
  },
  sub() {
    wx.showLoading({
      title: "下单中",
    });
    const { address_id } = this.data;
    if (address_id === "") {
      toast("请选择收货地址");
    } else {
      PostOrder({ address_id })
        .then((res: any) => {
          console.log(res);
          if (res.statusCode === 200) {
            toast("下单成功");
            wx.reLaunch({
              url: "/pages/order/order",
            });
          } else {
            toast("下单失败");
          }
        })
        .catch((err: (arg0: any) => void) => {
          err(err);
        });
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
